package com.hghivln.controller.doctor;


import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import com.hghivln.config.pay.AlipayConfig;
import com.hghivln.constants.Constants;
import com.hghivln.service.OrderChargeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付
 */
@RestController
@RequestMapping("pay")
public class PayController {

    @Reference
    private OrderChargeService orderChargeService;

    @RequestMapping("callback/{orderId}")
    public void callback(@PathVariable @Validated @NotBlank(message = "收费ID不能为空") String orderId, HttpServletRequest request) {
        Map<String, String> parameterMap = this.getParameterMap(request);
        String tradeStatus = parameterMap.get("trade_status");
        if (tradeStatus.equals("TRADE_SUCCESS")) {
            try {
                // 验证是否为支付宝发来的信息
                boolean singVerified = AlipaySignature.rsaCheckV1(parameterMap, AlipayConfig.alipayPublicKey, AlipayConfig.charset, AlipayConfig.signType);
                if (singVerified) {
                    String refundFee = parameterMap.get("refund_fee");
                    if (StringUtils.isNotBlank(refundFee)) {
                        // 退费
                        System.out.println("退费成功：退费的子订单ID为：" + parameterMap.get("out_biz_no"));
                    } else {
                        // 支付
                        String tradeNo = parameterMap.get("trade_no");
                        orderChargeService.paySuccess(orderId, Constants.PAY_TYPE_1, tradeNo);
                    }
                }

            } catch (AlipayApiException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("二维码已扫描，等待支付");
        }

    }


    /**
     * 获取request中的参数集合转Map
     * Map<String,String> parameterMap = RequestUtil.getParameterMap(request)
     *
     * @param request
     * @return
     */
    private Map<String, String> getParameterMap(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        return map;
    }
}
